#!/bin/bash
#description: auto install redis server
SOFTDIR="/opt/tools"
VER="3.2.9"
RDIR="/data/redis"
IP="$(ifconfig eth0|grep inet|grep -v inet6|awk '{print $2}')"
PORT=$1
PASSWORD="Aa123"
if [ $# -lt 1 ];then
	echo "$0 <PORT>"
	exit 1
fi
yum -y install gcc gcc-c++ make 
#check if user is root
if [ $(id -u) != "0" ];then
	echo "Error: You must be root to run this script, please use root to install"
	exit 2
fi
[ ! -d $SOFTDIR ] && mkdir -p $SOFTDIR
#download redis and install redis
function install_redis(){
	#create redis user
	user=$(cat /etc/passwd|grep "^redis\>"|awk -F: '{print $1}')
	if [ -z "$user" ];then
		useradd -u 1053 -s /bin/false -M redis
	else
		echo "user redis already exists"
	fi
	cd $SOFTDIR 
	[ ! -f redis-${VER}.tar.gz ] && wget http://download.redis.io/releases/redis-${VER}.tar.gz
	tar xf redis-${VER}.tar.gz -C /usr/local
	cd /usr/local && ln -s redis-${VER} redis
	cd /usr/local/redis && make  && make PREFIX=/usr/local/redis install 
}
function configure_redis(){
	[ ! -d $RDIR ] && mkdir -p $RDIR 
	mkdir -p ${RDIR}/members-${PORT}/{conf,data,log,pid}
	cd ${RDIR}/members-${PORT}/conf
	[ ! -f redis.conf ] && cp /usr/local/redis/redis.conf ${RDIR}/members-${PORT}/conf/
	chown -R redis.redis ${RDIR}/members-${PORT}
	[ ! -f redis.conf.ori ] && cp redis.conf{,.ori}
	cat >${RDIR}/members-${PORT}/conf/redis.conf <<EOF
	################################## NETWORK #####################################
	port $PORT
	bind $IP
	protected-mode yes
	tcp-backlog 65535
	timeout 0
	tcp-keepalive 300
	################################# GENERAL #####################################
	daemonize yes
	pidfile ${RDIR}/members-${PORT}/pid/redis.pid
	loglevel notice
	logfile ${RDIR}/members-${PORT}/log/redis.log
	databases 16
	################################ SNAPSHOTTING  ################################
	save 900 1
	save 300 10
	save 60 10000
	stop-writes-on-bgsave-error yes
	rdbcompression yes
	rdbchecksum yes
	dbfilename dump.rdb
	dir ${RDIR}/members-${PORT}/data
	################################# REPLICATION #################################
	# slaveof <masterip> <masterport>
	# masterauth <master-password>
	slave-serve-stale-data yes
	slave-read-only yes
	repl-diskless-sync no
	repl-diskless-sync-delay 5
	# repl-timeout 60
	repl-disable-tcp-nodelay no
	# repl-backlog-size 1mb
	# repl-backlog-ttl 3600
	slave-priority 100
	################################## SECURITY ###################################
	requirepass $PASSWORD
	rename-command FLUSHALL ""
	rename-command FLUSHDB ""
	rename=command KEYS ""	
	################################### LIMITS ####################################
	#maxclients 20000
	#maxmemory 32G
	#maxmemory-policy volatile-lru
	############################## APPEND ONLY MODE ###############################	
	no-appendfsync-on-rewrite no
	appendonly no
	appendfilename "appendonly.aof"
	appendfsync everysec
	auto-aof-rewrite-percentage 100
	auto-aof-rewrite-min-size 64mb
	aof-load-truncated yes
	aof-rewrite-incremental-fsync yes
	################################ LUA SCRIPTING  ###############################
	lua-time-limit 5000
        ################################## SLOW LOG ###################################
	slowlog-log-slower-than 10000
	slowlog-max-len 128
	################################ LATENCY MONITOR ##############################
	latency-monitor-threshold 0
	############################# EVENT NOTIFICATION ##############################
	notify-keyspace-events ""
	############################### ADVANCED CONFIG ###############################
	hash-max-ziplist-entries 512
	hash-max-ziplist-value 64
	list-max-ziplist-size -2
	list-compress-depth 0
	set-max-intset-entries 512
	zset-max-ziplist-entries 128
	zset-max-ziplist-value 64
	hll-sparse-max-bytes 3000
	activerehashing yes
	client-output-buffer-limit normal 0 0 0
	client-output-buffer-limit slave 256mb 64mb 60
	client-output-buffer-limit pubsub 32mb 8mb 60
	hz 10
EOF
	echo 'export PATH=$PATH:/usr/local/redis/bin' >>/etc/profile
	source /etc/profile
	sudo -u redis `which redis-server` /data/redis/members-${PORT}/conf/redis.conf

}
main(){
	install_redis
	configure_redis
}
main
